package com.pg.controller.dd;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pg.entity.EipTemp;
import com.pg.entity.cj.Jjd;
import com.pg.entity.dd.EipOperation;
import com.pg.entity.dd.EipProductionOrder;
import com.pg.service.EipProductionOrderService;
import com.pg.service.EipTempService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 生产订单前端控制器
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@Api(tags = "生产订单")
@RestController
@RequestMapping("/eipProductionOrder")
@Slf4j
public class EipProductionOrderController extends JeecgController<EipProductionOrder, EipProductionOrderService> {

    @Autowired
    private EipTempService eipTempService;

    @Autowired
    private EipProductionOrderService eipProductionOrderService;

    /**
     * 添加 生产订单
     *
     * @param eipProductionOrder 生产订单
     * @return 是否成功
     */
    @ApiOperation("添加 生产订单")
    @AutoLog(value = "添加 生产订单")
    @PostMapping("/add")
    public Result<?> add(@RequestBody EipProductionOrder eipProductionOrder) {
        eipProductionOrder.setPuid(UUID.randomUUID().toString());
        eipProductionOrderService.save(eipProductionOrder);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑 生产订单
     *
     * @param eipProductionOrder 工单
     * @return 是否成功
     */
    @ApiOperation("编辑 生产订单")
    @AutoLog(value = "编辑 生产订单")
    @PostMapping("/edit")
    public Result<?> edit(@RequestBody EipProductionOrder eipProductionOrder) {
        eipProductionOrderService.updateById(eipProductionOrder);
        return Result.ok("修改成功！");
    }

    /**
     * 临时编辑 生产订单
     *
     * @param eipProductionOrder 生产订单
     * @return 是否成功
     */
    @ApiOperation("临时编辑 生产订单")
    @AutoLog(value = "DataCheck")
    @RequestMapping(method = RequestMethod.POST, value = "/editTemp")
    public Result<?> editTemp(@Valid @RequestBody EipProductionOrder eipProductionOrder, HttpServletRequest req) {
        String userName = (String) req.getSession().getAttribute("userName");
        EipTemp eipTemp = new EipTemp();
        eipTemp.setDataType("生产订单");
        eipTemp.setModUser(userName);
        eipTemp.setModTime(new Date());
        eipTemp.setLinkPUID(eipProductionOrder.getPuid());
        eipTemp.setDetailedInformation(JSONObject.toJSONString(eipProductionOrder));
        try {
            eipProductionOrderService.updateProductionOrder(eipTemp);
            return Result.ok("修改成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("修改失败！");
        }
    }

    /**
     * 通过id删除 生产订单
     *
     * @param id 全库唯一码
     * @return 是否成功
     */
    @ApiOperation("通过id删除 生产订单")
    @AutoLog(value = "通过id删除 生产订单")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        eipProductionOrderService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除 生产订单
     *
     * @param ids 全库唯一码集合
     * @return 是否成功
     */
    @ApiOperation("批量删除 生产订单")
    @AutoLog(value = "批量删除 生产订单")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.eipProductionOrderService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id逻辑删除 生产订单
     *
     * @param id 全库唯一码
     * @return 是否成功
     */
    @ApiOperation("通过id逻辑删除 生产订单")
    @AutoLog(value = "通过id逻辑删除 生产订单")
    @DeleteMapping(value = "/logicalDelete")
    public Result<?> logicalDelete(@RequestParam(name = "id", required = true) String id) {
        eipProductionOrderService.logicalDelete(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量逻辑删除 生产订单
     *
     * @param ids 全库唯一码集合
     * @return 是否成功
     */
    @ApiOperation("批量逻辑删除 生产订单")
    @AutoLog(value = "批量逻辑删除 生产订单")
    @DeleteMapping(value = "/logicalDeleteBatch")
    public Result<?> logicalDeleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        eipProductionOrderService.logicalDeleteBatch(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询 生产订单
     *
     * @param puid 全库唯一码
     * @return 是否成功
     */
    @ApiOperation("通过id查询 生产订单")
    @AutoLog(value = "DataCheck")
    @RequestMapping(method = RequestMethod.GET, value = "/selectByPuid")
    public Result<?> selectByPuid(@RequestParam(name = "puid") String puid) {
        EipProductionOrder byId;
        byId = eipTempService.selectProductionOrderByLinkPUID(puid);
        if (byId == null) {
            byId = eipProductionOrderService.selectByPuid(puid);
            if (byId == null) {
                return Result.error("未找到对应数据");
            }
        }
        return Result.ok(byId);
    }

    /**
     * 分页列表查询 生产订单
     *
     * @param eipProductionOrder  生产订单
     * @param planStartDateBegin  起始时间
     * @param planStartDateFinish 结束时间
     * @param pageNo              起始页
     * @param pageSize            每页条数
     * @param req                 参数
     * @return 分页数据
     */
    @ApiOperation("分页列表查询 生产订单")
    @AutoLog(value = "QueryExport")
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public Result<?> selectForList(
            EipProductionOrder eipProductionOrder,
            @RequestParam(name = "planStartDateBegin", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date planStartDateBegin,
            @RequestParam(name = "planStartDateFinish", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date planStartDateFinish,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        if ("undefined".equals(eipProductionOrder.getPoItemId())) {
            eipProductionOrder.setPoItemId(null);
        }
        Page<EipProductionOrder> page = new Page<EipProductionOrder>(pageNo, pageSize);
        IPage<EipProductionOrder> pageList = eipProductionOrderService.selectForList(page, eipProductionOrder, planStartDateBegin, planStartDateFinish);
        return Result.ok(pageList);
    }

    /**
     * 分页列表查询 生产订单
     *
     * @param eipProductionOrder  生产订单
     * @param planStartDateBegin  起始时间
     * @param planStartDateFinish 结束时间
     * @param pageNo              起始页
     * @param pageSize            每页条数
     * @param req                 参数
     * @return 分页数据
     */
    @ApiOperation("分页列表查询 生产订单")
    @AutoLog(value = "DataCheck")
    @RequestMapping(method = RequestMethod.GET, value = "/checkList")
    public Result<?> checkList(
            EipProductionOrder eipProductionOrder,
            @RequestParam(name = "planStartDateBegin", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date planStartDateBegin,
            @RequestParam(name = "planStartDateFinish", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date planStartDateFinish,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        if ("undefined".equals(eipProductionOrder.getPoItemId())) {
            eipProductionOrder.setPoItemId(null);
        }
        Page<EipProductionOrder> page = new Page<EipProductionOrder>(pageNo, pageSize);
        IPage<EipProductionOrder> pageList = eipProductionOrderService.selectForList(page, eipProductionOrder, planStartDateBegin, planStartDateFinish);
        return Result.ok(pageList);
    }

    /**
     * 通过excel导入数据 生产订单
     *
     * @param request  请求
     * @param response 响应
     * @return 是否成功
     */
    @ApiOperation("通过excel导入数据 生产订单")
    @AutoLog(value = "通过excel导入数据 生产订单")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EipProductionOrder.class);
    }

    /**
     * 导出excel 生产订单
     *
     * @param request            请求
     * @param eipProductionOrder 生产订单
     */
    @ApiOperation("导出excel 生产订单")
    @AutoLog(value = "QueryExport")
    @RequestMapping(method = RequestMethod.GET, value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EipProductionOrder eipProductionOrder) {
        return super.exportXls(request, eipProductionOrder, EipProductionOrder.class, "eip_production_order");
    }
}
